﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Commands.User.Commands.Order;
using DotNet.Common.Command;
using ZLCMS.Commands.User.Util;
using NHibernate;
using ZLCMS.Commands.Data;
using System.IO;
using Newtonsoft.Json;
using ZLCMS.Commands.User.Commands.Reconciliations;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Reconciliations
{
    public partial class BillPaymentList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected string uAcc;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ分页控件
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                IList<SysDepartmentInfo> deptList = session.CreateQuery(" from SysDepartmentInfo where DeptCode<>'0001' ")
                    .List<SysDepartmentInfo>();
                deptList.Insert(0, new SysDepartmentInfo() { DeptCode = "-1", DeptName = "--请选择--" });
                DeptCode.DataSource = deptList;
                DeptCode.DataTextField = "DeptName";
                DeptCode.DataValueField = "DeptCode";
                DeptCode.DataBind();
            }
            LoadBillPaymentListParameter param = new LoadBillPaymentListParameter();
            param.BillWeeklyNo = Request["bno"];

            param.Start = string.IsNullOrEmpty(Request["start"]) ? "" : Request["start"];
            start.Text = string.Format("{0:yyyy-MM-dd}", Request["start"]);

            param.End = string.IsNullOrEmpty(Request["end"]) ? "" : Request["end"];
            end.Text = string.Format("{0:yyyy-MM-dd}", Request["end"]);

            uAcc = UserKit.GetUserAccount();

            if (AuthUtil.CheckAuthorization("BILL_MAN", "VIEW_ALL", uAcc))
            {
                param.DeptCode = string.IsNullOrEmpty(Request["deptcode"]) ? "-1" : Request["deptcode"];
                DeptCode.SelectedValue = param.DeptCode;
            }
            else if (AuthUtil.CheckAuthorization("BILL_MAN", "VIEW_OWNER", uAcc))
            {
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    param.DeptCode = session.CreateSQLQuery("select dept_code from sys_user_info where user_account=:acc")
                        .SetParameter("acc", uAcc).UniqueResult<string>();
                }
            }
            else
            {
                //没有权限访问
                param.DeptCode = "no_dept";
            }

            if (!string.IsNullOrEmpty(Request["exportexcel"]))
            {
                //设置分页大小
                param.PageSize = 9999;
                //设置页码
                param.PageIndex = 1;
            }
            else
            {
                //设置分页大小
                param.PageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;
                //设置页码
                param.PageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;

                ICommandResult<LoadBillPaymentListResult> result = new LoadBillPaymentListCommand().Execute(param);
                if (!result.Error)
                {

                    grid.DataSource = result.Result.List;
                    grid.DataBind();
                    _framePage.RecordCount = result.TotalCount;
                }
                else
                {
                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 300,
                        message = "参数错误!",
                        callbackType = "closeCurrent",
                        navTabId = "BillPaymentList"
                    });
                }
            }

        }

    }
}